package com.emergency.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emergency.data.entity.*;
import com.emergency.data.vo.DataVO;
import com.emergency.data.vo.ResourceVO;
import com.emergency.data.vo.StreetVO;
import com.emergency.mapper.*;
import com.emergency.service.StreetService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (Street)街道服务实现类
 *
 * @author makejava
 * @since 2020-09-17
 */
@Service
public class StreetServiceImpl extends ServiceImpl<StreetMapper, Street> implements StreetService {

    @Resource
    private StreetMapper streetMapper;
    @Resource
    private EmergencySuppliesPointMapper emergencySuppliesPointMapper;
    @Resource
    private SettlementPointMapper settlementPointMapper;
    @Resource
    private LinkmanPointMapper linkmanPointMapper;
    @Resource
    private RescueTeamMapper rescueTeamMapper;


    @Override
    public List<StreetVO> getStreetList(Integer type) {
        //查询条件
        QueryWrapper<Street> query = new QueryWrapper<>();
        if (type != null) {
            query.eq("type", type);
        }
        return streetMapper.selectList(query).stream().map(c -> {
            StreetVO vo = new StreetVO();
            BeanUtil.copyProperties(c, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public Map<String, Object> dataStatistics() {
        Map<String,Object> map = new HashMap<>();
        List<String> nameList = new ArrayList<>();
        List<DataVO> dataList = new ArrayList<>();
        List<StreetVO> streetList = getStreetList(1);
        if(streetList != null && streetList.size() > 0){
            streetList.forEach(c->{
                nameList.add(c.getStreetName());
                DataVO dataVO = new DataVO();
                dataVO.setName(c.getStreetName());
                dataVO.setType("bar");
                dataVO.setLabel("seriesLabel");
                List<Integer> list = new ArrayList<>();
                //查询条件
                QueryWrapper<EmergencySuppliesPoint> equery = new QueryWrapper<>();
                equery.eq("street_id", c.getId());
                equery.eq("delete_flag", 0);
                QueryWrapper<SettlementPoint> squery = new QueryWrapper<>();
                squery.eq("street_id", c.getId());
                squery.eq("delete_flag", 0);
                QueryWrapper<LinkmanPoint> lquery = new QueryWrapper<>();
                lquery.eq("street_id", c.getId());
                lquery.eq("delete_flag", 0);
                // 应急物资点统计
                Integer eCount = emergencySuppliesPointMapper.selectCount(equery);
                // 安置点统计
                Integer sCount = settlementPointMapper.selectCount(squery);
                // 负责人联络点
                Integer lCount = linkmanPointMapper.selectCount(lquery);
                list.add(eCount);
                list.add(sCount);
                list.add(lCount);
                dataVO.setData(list);
                dataList.add(dataVO);
            });
        }
        map.put("nameList",nameList);
        map.put("dataList",dataList);
        return map;
    }

    @Override
    public Map<String, Object> getResourceList() {
        Map<String,Object> map = new HashMap<>();
        List<ResourceVO> resourceVOList = new ArrayList<>();

        List<EmergencySuppliesPoint> emergencySuppliesPoints = emergencySuppliesPointMapper.selectList(null);
        emergencySuppliesPoints.forEach(c->{
                ResourceVO resourceVO = new ResourceVO();
                resourceVO.setId(c.getId());
                resourceVO.setX(c.getX());
                resourceVO.setY(c.getY());
                resourceVO.setType(1);
                resourceVOList.add(resourceVO);
        });

        List<SettlementPoint> settlementPoints = settlementPointMapper.selectList(null);
        settlementPoints.forEach(c->{
                ResourceVO resourceVO = new ResourceVO();
                resourceVO.setId(c.getId());
                resourceVO.setX(c.getX());
                resourceVO.setY(c.getY());
                resourceVO.setType(2);
                resourceVOList.add(resourceVO);
        });

        List<RescueTeam> rescueTeams = rescueTeamMapper.selectList(null);
        rescueTeams.forEach(c->{
            ResourceVO resourceVO = new ResourceVO();
            resourceVO.setId(c.getId());
            resourceVO.setX(c.getX());
            resourceVO.setY(c.getY());
            resourceVO.setType(3);
            resourceVOList.add(resourceVO);
        });

        List<LinkmanPoint> linkmanPoints = linkmanPointMapper.selectList(null);
        linkmanPoints.forEach(c->{
                ResourceVO resourceVO = new ResourceVO();
                resourceVO.setId(c.getId());
                resourceVO.setX(c.getX());
                resourceVO.setY(c.getY());
                resourceVO.setType(4);
                resourceVOList.add(resourceVO);
        });

        map.put("list",resourceVOList);
        map.put("emergencySuppliesPointTotal",emergencySuppliesPoints.size());
        map.put("settlementPointTotal",settlementPoints.size());
        map.put("rescueTeamTotal",rescueTeams.size());
        map.put("linkmanPointTotal",linkmanPoints.size());
        return map;
    }
}
